package com.glory.wxmemsicpda.adapter

import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.base.DataBindBaseViewHolder
import com.glory.wxmemsicpda.bean.DataAcquisitionBean
import com.glory.wxmemsicpda.databinding.AdapterDataAcquisitionBinding
import java.lang.Exception

class DataAcquisitionAdapter(
    data: List<DataAcquisitionBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO?>?,
    val quaNum: TextView,
    val undesirableNum: TextView,
    var mainQty: Int
) :
    BaseQuickAdapter<DataAcquisitionBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO?, DataBindBaseViewHolder>(
        R.layout.adapter_data_acquisition,
        data as MutableList<DataAcquisitionBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO?>
    ) {

    @SuppressLint("SetTextI18n")
    override fun convert(
        helper: DataBindBaseViewHolder,
        item: DataAcquisitionBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO?
    ) {
        var binding: AdapterDataAcquisitionBinding =
            helper.getDataBinding() as AdapterDataAcquisitionBinding
        binding.dataAcquisition = item
        binding.noNum.text = (helper.adapterPosition + 1).toString()
        binding.num.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                item?.value = s.toString()
                var num = 0
                try {
                    data.forEach {
                        if (it?.name != "BIN01" && it!!.value.isNotEmpty()) {
                            num += it.value.toInt()
                        }
                    }
                    undesirableNum.text = num.toString()
                    quaNum.text = (mainQty - num).toString()
                } catch (e: Exception) {
                    undesirableNum.text = "0"
                    quaNum.text = "0"
                }


            }

        })

        binding.executePendingBindings()
    }

    fun setMainQty(mainqty: String) {
        if (mainqty.isNotEmpty()) {
            this.mainQty = mainqty.toInt()
        }
    }


}